package com.yunhui.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunhui.entity.RouteJb;
import com.yunhui.entity.RouteJbEs;
import com.yunhui.mapper.MerchantMapper;
import com.yunhui.mapper.RouteJbMapper;

import com.yunhui.service.RouteJbService;


import com.yunhui.util.PageSupport;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  行程基本信息的业务处理的层实现类
 * </p>
 *
 * @author WGH
 * @since 2019-12-09
 */
@Service
public class RouteJbServiceImpl  extends ServiceImpl<RouteJbMapper, RouteJb> implements RouteJbService {

    @Resource
    private RouteJbMapper routeJbMapper;

    @Resource
    private MerchantMapper merchantMapper;

    

    /**
     * 根据条件查询所有数据
     * @param routeJb
     * @return
     */
    @Override
    public List<RouteJb> getListRouteJb(RouteJb routeJb) {
        QueryWrapper<RouteJb> qw=new QueryWrapper<RouteJb>();
        //如果status不为空并且大于0则条件加上status
        if (routeJb.getStatus()!=null&&routeJb.getStatus()>0)qw.eq("status",routeJb.getStatus());
        //如果title不为空则条件加上title
        if (routeJb.getTitle()!=null)qw.like("title",routeJb.getTitle());
        //如果mId不为空并且大于0则条件加上mId
        if (routeJb.getmId()!=null&&routeJb.getmId()>0)qw.eq("mId",routeJb.getmId());
        //按照创建时间排序
        qw.orderByDesc("createDate");
        List<RouteJb> list=null;
        try {
            list=routeJbMapper.selectList(qw );
        }catch (Exception e){
            e.printStackTrace();
        }
        for (RouteJb ro:list
             ) {
            ro.setStDate();
        }
        return list;
    }

    /**
     * 分页查询
     * @param routeJb
     * @param pageSupport
     * @return
     */
    @Override
      public List<RouteJb> getPageRouteJb(RouteJb routeJb, PageSupport pageSupport) {

        List<RouteJb> list=null;
        Integer index=(pageSupport.getCurrentPageNo()-1)*pageSupport.getPageSize();
        try {
            list=routeJbMapper.getPageList(index,pageSupport.getPageSize(),routeJb);
            System.out.println(list.size());
            for (RouteJb ro:list
            ) {
                ro.setProName(merchantMapper.selectById(ro.getmId()).getMerchantName());
                ro.setStDate();
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        return list;
    }

    /**
     * 插入数据
     * @param routeJb
     * @return
     */
    @Override
    public int insert(RouteJb routeJb) {
        return routeJbMapper.insert(routeJb);
    }

    /**
     * 根据条件查询总数
     * @param routeJb
     * @return
     */
    @Override
    public int getCount(RouteJb routeJb) {
        int i=0;
        try {
            i=routeJbMapper.getCount(routeJb);
        }catch (Exception e){
            e.printStackTrace();
        }
        return i;
    }

    /**
     * 根据id修改状态
     * @param id
     * @param status
     * @return
     */
    @Override
    public int updateById(Integer id, Integer status) {
        int num=0;
        try {
            num=routeJbMapper.updateById(id,status);
        }catch (Exception e){
         e.printStackTrace();
        }
        return num;
    }

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    @Override
    public RouteJb selectByIds(Integer id) {
        RouteJb routeJb =null;
        try{
            routeJb=routeJbMapper.selectById(id);
        }catch (Exception e){
            e.printStackTrace();
        }

        return routeJb;
    }


    /**
     * 使用ES分词搜索引擎
     * @param title  标题
     * @param destination 目的地
     * @param travelType1 类型
     * @return
     */
    @Override
    public List<RouteJbEs> findByEs(String title, String destination, Integer travelType1,PageSupport page,Integer min,Integer max,Integer pai) {
		/*
		 * List<RouteJbEs> list=new ArrayList<>(); try {
		 * 
		 * @SuppressWarnings("unchecked") List<RouteJbEs>
		 * routeJbList=routeJbElasticsearch.select(title,destination,travelType1,
		 * travelType1,title,destination);
		 * 
		 * 
		 * List<RouteJbEs> listc=new ArrayList<>(); //时间长度控制 for (RouteJbEs
		 * routeJb:routeJbList ) { if(min!=null&&max!=null){
		 * if(routeJb.getDays()>=min&&routeJb.getDays()<=max){ listc.add(routeJb); }
		 * }else{ listc=routeJbList; break; } } List<RouteJbEs> lists=new
		 * ArrayList<>(listc);
		 * 
		 * if(pai==1){ //根据单价进行冒泡排序 正序 从小到大 for (int i=0;i<lists.size();i++){ for (int
		 * j=0;j<i;j++){
		 * if(lists.get(j).getPrice().compareTo(lists.get(i).getPrice())==1){ RouteJbEs
		 * es=lists.get(j); lists.set(j,lists.get(i)); lists.set(i,es); } }
		 * 
		 * } }
		 * 
		 * if(pai==2){ //根据单价进行冒泡排序 倒叙 从大到小 for (int i=lists.size()-1;i>=0;i--){ for
		 * (int j=i-1;j>=0;j--){
		 * if(lists.get(j).getPrice().compareTo(lists.get(i).getPrice())==-1){ RouteJbEs
		 * es=lists.get(j); lists.set(j,lists.get(i)); lists.set(i,es); } } } }
		 * 
		 * 
		 * 
		 * //计算可用总条数 page.setTotalCount(lists.size());
		 * 
		 * //分页数量控制 int i=0; //获取到的数量 每次获取到+1 int index=1;//位置 int
		 * go=(page.getCurrentPageNo()-1)*page.getPageSize(); //开始位置 for (RouteJbEs
		 * routeJb:lists ) { if(index>go){ i++; list.add(routeJb); }
		 * if(i==page.getPageSize()){ break; } index++; }
		 * 
		 * }catch (Exception e ){ e.printStackTrace(); }
		 */
        return null;
    }

    /**
     * 根据Id修改所有字段
     * @param routeJb
     * @return
     */
    @Override
    public int updateByIdQun(RouteJb routeJb) {
        int num=0;
        try {
            num=routeJbMapper.updateByIdQun(routeJb);
        }catch (Exception e){
            e.printStackTrace();
        }
        return num;
    }

    /**
     * 根据ID删除
     * @param id
     * @return
     */
    @Override
    public int deleteById(Integer id) {
        int num =0;
        try {
            num=routeJbMapper.deleteById(id);
        }catch (Exception e){
            e.printStackTrace();
        }
        return num;
    }


}
